package net.jielan188.hd.controller.user;

//import net.jielan188.hd.bean.user.User;
import net.jielan188.hd.service.user.UserServiceT;
import net.jielan188.hd.util.Log;
import net.jielan188.hd.util.common.RequestUtil;
import net.jielan188.hd.util.common.md5.CryptographyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xfyd.model.User;
import xfyd.spi.user.UserServiceI;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 用户
 * Created by hgg on 2017-01-23.
 */
@Controller
@RequestMapping(value = "/user")
public class UserAct implements Serializable{

    @Autowired
    private UserServiceT userService;

    @Resource
    private UserServiceI userServiceN;

    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping(value = "/login")
    public String to_login(){

        return "user/login.html";
    }

//    /**
//     * 校验登录
//     * @param request
//     * @return
//     */
//    @RequestMapping(value = "/check_login")
//    @ResponseBody
//    public Object check_login( HttpServletRequest request ){
//
//        try {
//
//            //【1】封装参数
//            Map<String,Object> requestMap = RequestUtil.getGetData( request );
//            if( requestMap.get( "userName" ) == null || requestMap.get( "userPass" ) == null ){
//                return 0;
//            }
//
//            Subject currentUser = SecurityUtils.getSubject();
//            String md5Pwd = CryptographyUtil.md5( (String) requestMap.get( "userPass" ), (String) requestMap.get( "userName" ) );
//            UsernamePasswordToken token = new UsernamePasswordToken( (String) requestMap.get( "userName" ), md5Pwd );
//
//            //【2】校验信息
//            currentUser.login( token );
//
//            //【3】查询用户
//            User user = userService.getUserByUserName( (String)requestMap.get( "userName" ) );
//            if( user == null ){
//                return 0;
//            }
//            request.getSession().setAttribute( "currentUser",user );
//
//        }catch ( Exception e ){
//            Log.error( "【用户登录】：用户名或者密码错误" );
//            return 0;
//        }
//        return 1;
//    }

    @RequestMapping(value = "/listAll")
    public String userList( HttpServletRequest request ){

        List<User> users = userServiceN.allUsers();

        request.setAttribute( "users",users );
        return "user/list.html";
    }

}
